// AboutDlg.cpp : implementation of the CGroupsDlg class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#ifdef WIN32_PLATFORM_PSPC
#include "resourceppc.h"
#else
#include "resourcesp.h"
#endif
#include "draftinfodlg.h"
#include "utils.h"
#include "DraftsDb.h"
#include "DraftsDlg.h"

CDraftInfoDlg::CDraftInfoDlg()
    : m_Mode(Mode_NewDraft)
{
}

LRESULT CDraftInfoDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
    if (m_Mode == Mode_SelectDraft)
        AtlCreateMenuBar(m_hWnd, IDM_SELECT_CANCEL);
    else
        AtlCreateMenuBar(m_hWnd, IDM_OK_CANCEL);

#ifdef WIN32_PLATFORM_WFSP
    if (hMenuBar != NULL)
        AtlActivateBackKey(hMenuBar);
#endif

    m_DraftEdit.Attach(GetDlgItem(IDC_EDT_DRAFT));
    m_DraftEdit.LimitText(MAX_DRAFT);
    m_DraftEdit.SetWindowText(m_Draft);
    m_DraftEdit.SetFocus();

    m_StatusStc.Attach(GetDlgItem(IDC_STC_STATUS));
    m_StatusStc.GetWindowText(m_StatusFormat);

    UpdateUI();

    return bHandled = FALSE;
}

LRESULT CDraftInfoDlg::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
    m_DraftEdit.GetWindowText(m_Draft);
    return bHandled = FALSE;
}

LRESULT CDraftInfoDlg::OnMenuOk(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    StdCloseDialog(ID_MENU_OK);
    return 0;
}

LRESULT CDraftInfoDlg::OnMenuCancel(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    StdCloseDialog(ID_MENU_CANCEL);
    return 0;
}

LRESULT CDraftInfoDlg::OnMenuSelect(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    CDraftsDlg dlg;
    dlg.SetSelectMode();
    int ret = dlg.DoModal();
    if (ret == IDOK)
    {
        CDraftsDb DraftsDb;
        DraftsDb.Open();

        CString Draft;
        DWORD Err = DraftsDb.GetFromId(dlg.GetDraftId(), &Draft, NULL);

        DraftsDb.Close();

        if (Err != ERROR_SUCCESS)
        {
            ATLTRACE(_T("Unable to get db index\n"));
            return 0;
        }

        m_DraftEdit.SetWindowText(Draft);
    }

    return 0;
}

LRESULT CDraftInfoDlg::OnEdtDraftChange(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    UpdateUI();
    return 0;
}

void CDraftInfoDlg::UpdateUI(void)
{
    bool Enable = m_DraftEdit.GetWindowTextLength() > 0;
    Utils::EnableMenuItem(this->m_hWnd, ID_MENU_OK, Enable);

    CString s;
    s.Format(m_StatusFormat, m_DraftEdit.GetWindowTextLength());
    m_StatusStc.SetWindowText(s);
}
